package com.yunji.news.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.yunji.news.common.Result;
import com.yunji.news.entity.AdmissionResult;
import com.yunji.news.service.AdmissionResultService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 录取结果控制器
 *
 * @author yunji
 * @since 2024-11-26
 */
@RestController
@RequestMapping("/admission")
@CrossOrigin
public class AdmissionResultController {

    @Autowired
    private AdmissionResultService admissionService;

    /**
     * 分页查询录取结果（管理端）
     */
    @GetMapping("/admin")
    public Result<IPage<AdmissionResult>> getAdmissions(
            @RequestParam(defaultValue = "1") Integer page,
            @RequestParam(defaultValue = "20") Integer size,
            @RequestParam(required = false) Long schoolId,
            @RequestParam(required = false) Long majorId,
            @RequestParam(required = false) String admissionStatus,
            @RequestParam(required = false) String keyword) {
        
        IPage<AdmissionResult> result = admissionService.getAdmissionPage(
            page, size, schoolId, majorId, admissionStatus, keyword);
        return Result.success(result);
    }

    /**
     * 获取公开的录取名单（前端展示）
     */
    @GetMapping("/public")
    public Result<List<AdmissionResult>> getPublicAdmissions(
            @RequestParam(required = false) Long schoolId,
            @RequestParam(required = false) Long majorId,
            @RequestParam(required = false) String admissionStatus) {
        
        List<AdmissionResult> results = admissionService.getPublicAdmissionList(
            schoolId, majorId, admissionStatus);
        return Result.success(results);
    }

    /**
     * 查询学生录取状态
     */
    @GetMapping("/student/{idCard}")
    public Result<AdmissionResult> getStudentAdmission(
            @PathVariable String idCard,
            @RequestParam(required = false) Long schoolId) {
        
        AdmissionResult result = admissionService.getStudentAdmissionResult(idCard, schoolId);
        if (result != null) {
            return Result.success(result);
        } else {
            return Result.error("未查询到录取信息");
        }
    }

    /**
     * 生成录取结果
     */
    @PostMapping("/generate")
    public Result<String> generateAdmissionResults(
            @RequestParam Long schoolId,
            @RequestParam Long majorId,
            @RequestParam Integer admissionCount) {
        
        boolean success = admissionService.generateAdmissionResults(schoolId, majorId, admissionCount);
        if (success) {
            return Result.success("录取结果生成成功");
        } else {
            return Result.error("录取结果生成失败");
        }
    }

    /**
     * 发布录取结果
     */
    @PutMapping("/publish")
    public Result<String> publishAdmissionResults(@RequestParam Long[] ids) {
        boolean success = admissionService.publishAdmissionResults(ids);
        if (success) {
            return Result.success("录取结果发布成功");
        } else {
            return Result.error("录取结果发布失败");
        }
    }

    /**
     * 学生确认录取
     */
    @PutMapping("/{id}/confirm")
    public Result<String> confirmAdmission(@PathVariable Long id) {
        boolean success = admissionService.confirmAdmission(id);
        if (success) {
            return Result.success("录取确认成功");
        } else {
            return Result.error("录取确认失败");
        }
    }

    /**
     * 批量更新录取状态
     */
    @PutMapping("/batch/status")
    public Result<String> batchUpdateStatus(
            @RequestParam Long[] ids,
            @RequestParam String admissionStatus) {
        
        boolean success = admissionService.batchUpdateAdmissionStatus(ids, admissionStatus);
        if (success) {
            return Result.success("批量更新成功");
        } else {
            return Result.error("批量更新失败");
        }
    }

    /**
     * 发送录取通知
     */
    @PostMapping("/{id}/notice")
    public Result<String> sendNotice(@PathVariable Long id) {
        boolean success = admissionService.sendAdmissionNotice(id);
        if (success) {
            return Result.success("录取通知发送成功");
        } else {
            return Result.error("录取通知发送失败");
        }
    }
}